﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using OnlineVideos.Entities;
using Common.Library;
using OnlineVideos.Data;
using System.ComponentModel;
using Common.Utilities;
using OnlineVideos.UI;

namespace OnlineVideos
{
    public partial class FavouriteComedy : UserControl
    {
        #region GLobal Declaration

        List<ShowLinks> showFavVideos = null;
        private bool IsDataLoaded;

        #endregion

        #region Constructor
        public FavouriteComedy()
        {
            InitializeComponent();
            IsDataLoaded = false;
            showFavVideos = new List<ShowLinks>();
        }
        #endregion

        #region PageLoad
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsDataLoaded == false)
                {
                    GetPageDataInBackground();
                    IsDataLoaded = true;
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in UserControl_Loaded Method In FavoriteVideos.cs file.", ex);
            }
        }
        #endregion               

        #region "Common Methods"
        private void GetPageDataInBackground()
        {
            try
            {
                BackgroundHelper bwHelper = new BackgroundHelper();
                bwHelper.AddBackgroundTask(
                                             (object s, DoWorkEventArgs a) =>
                                             {
                                                 a.Result = FavoritesManager.GetFavoriteLinks();
                                             },
                                                   (object s, RunWorkerCompletedEventArgs a) =>
                                                   {
                                                       showFavVideos = (List<ShowLinks>)a.Result;
                                                       if (showFavVideos.Count > 0)
                                                       {
                                                           lbxFavoritecomedy.ItemsSource = showFavVideos;

                                                           tblkFavNoComedy.Visibility = Visibility.Collapsed;
                                                       }
                                                       else
                                                       {
                                                           tblkFavNoComedy.Text = "No " + AppResources.FavoriteComedyPivotTitle + " available";
                                                           tblkFavNoComedy.Visibility = Visibility.Visible;
                                                       }
                                                   }
                                                 );
                bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in GetPageDataInBackground Method In FavoriteComedy.cs file.", ex);
            }
        }

        #endregion

        #region Events
        private void lbxFavoritecomedy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxFavoritecomedy.SelectedIndex == -1)
                    return;
                AppSettings.LinkTitle = (lbxFavoritecomedy.SelectedItem as ShowLinks).Title.ToString();
                AppSettings.LinkType = (lbxFavoritecomedy.SelectedItem as ShowLinks).LinkType.ToString();
                string Url = (lbxFavoritecomedy.SelectedItem as ShowLinks).LinkUrl;
                // MyToolkit.Multimedia.YouTube.LoadYoutubeVideo(Url);
                if (ResourceHelper.AppName == Apps.Video_Mix.ToString())
                {
                    PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
                else if (ResourceHelper.AppName == Apps.Web_Media.ToString())
                {
                    DownloadManagerHelper.Playvideo(int.Parse((lbxFavoritecomedy.SelectedItem as ShowLinks).ShowID.ToString()));
                }
                else
                {
                    PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                    Page p = frame.Content as Page;
                    p.NavigationService.Navigate(new Uri("/Views/Youtube.xaml?myid=" + Url, UriKind.Relative));
                    //YoutubeApi.Youtube.PlayYoutubeVideo(Url);
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxFavoritecomedy_SelectionChanged Method In FavouriteComedy.cs file.", ex);
            }
            lbxFavoritecomedy.SelectedIndex = -1;
        }

        private void btnFavComedy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbxFavoritecomedy, sender as MenuItem, LinkType.Comedy);
                PageHelper.NavigateTo(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnFavComedy_Click Method In FavoriteVideos.cs file.", ex);
            }
        }

        #endregion        

        

        
    }
}
